#include<vector>
using namespace std;

class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        int start = 0;
        while (start < nums.size()) {
            if (nums[start] <= 0) {
                ++start;
                continue;
            }
            int index = nums[start] - 1;
            if (nums[index] > 0) {
                nums[start] = nums[index];
                nums[index] = 0;
            }
            else
                nums[start] = 0;
            --nums[index];
        }
        vector<int> res;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == -2)
                res.push_back(i + 1);
        }
        return res;
    }
};